package com.hui.auth.service.impl;

import com.hui.auth.service.OnlineService;
import com.hui.utils.DateUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class OnlineServiceImpl implements OnlineService {
    @Override
    public List<Map> selectOnline() {
        DefaultWebSecurityManager securityManager = (DefaultWebSecurityManager) SecurityUtils.getSecurityManager();
        DefaultWebSessionManager sessionManager = (DefaultWebSessionManager) securityManager.getSessionManager();
        Collection<Session> sessions = sessionManager.getSessionDAO().getActiveSessions();//获取当前已登录的用户session列表
        List<Map> result = new ArrayList<Map>();
        for (Session session : sessions) {
            Map<String, Object> sInfo = new HashMap<String, Object>();
            sInfo.put("sessionId", session.getId());
            sInfo.put("host", session.getHost());
            sInfo.put("lastAccessTime", DateUtil.parseDateToString(session.getLastAccessTime()));
            sInfo.put("timeOut", session.getTimeout());
            result.add(sInfo);
        }
        return result;
    }

    @Override
    public boolean kickOut(String sessionId) {
        return false;
    }
}
